package com.hqsj;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;

/**
 * Created by huangying on 2017/3/6.
 */
@RestController
public class Controller {

    @GetMapping(value = "/users")
    public List<User> select(HttpServletRequest request,
                            HttpServletResponse response,
                            @RequestParam(value = "_page", defaultValue = "0") int page,
                            @RequestParam(value = "_limit", defaultValue = "0") int size) {

        System.out.println("==============select==================");

        Enumeration<String> e = request.getHeaderNames();
        while (e.hasMoreElements()) {
            String key = e.nextElement();
            System.out.println(key + ": " + request.getHeader(key));
        }

        List<User> list = new ArrayList<>();
        if (page < 1 || size < 1) {
            Iterator<User> it = userRepository.findAll().iterator();
            while (it.hasNext()) {
                list.add(it.next());
            }
        } else {
            Pageable pageable = new PageRequest(page - 1, size);
            Page<User> userPage = userRepository.findAll(pageable);
            list = userPage.getContent();
            response.setHeader("x-total-count", String.valueOf(userPage.getTotalElements()));
        }
        return list;
    }

    @DeleteMapping(value = "/users/{id}")
    public User delete(HttpServletRequest request, HttpServletResponse response, @PathVariable long id) {
        System.out.println("==============delete==================");

        Enumeration<String> e = request.getHeaderNames();
        while (e.hasMoreElements()) {
            String key = e.nextElement();
            System.out.println(key + ": " + request.getHeader(key));
        }

        User user = userRepository.findOne(id);
        userRepository.delete(user);
        return user;
    }

    @PatchMapping(value = "/users/{id}")
    public User update(@RequestBody String body, @PathVariable long id, HttpServletRequest request) {
        System.out.println("==============update==================");

        Enumeration<String> e = request.getHeaderNames();
        while (e.hasMoreElements()) {
            String key = e.nextElement();
            System.out.println(key + ": " + request.getHeader(key));
        }

        String name = (String) JSONUtil.getStringFromJSONObject(body, "name");
        String email = (String) JSONUtil.getStringFromJSONObject(body, "email");
        String website = (String) JSONUtil.getStringFromJSONObject(body, "website");
        User user = userRepository.findOne(id);
        System.out.println(user.getName());
        user.setName(name);
        user.setEmail(email);
        user.setWebsite(website);
        userRepository.save(user);
        return user;
    }

    @PostMapping(value = "/users")
    public User add(@RequestBody String body, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("==============add==================");

        Enumeration<String> e = request.getHeaderNames();
        while (e.hasMoreElements()) {
            String key = e.nextElement();
            System.out.println(key + ": " + request.getHeader(key));
        }

        String name = (String) JSONUtil.getStringFromJSONObject(body, "name");
        String email = (String) JSONUtil.getStringFromJSONObject(body, "email");
        String website = (String) JSONUtil.getStringFromJSONObject(body, "website");
        User user = new User();
        System.out.println(user.getName());
        user.setName(name);
        user.setEmail(email);
        user.setWebsite(website);
        userRepository.save(user);
        return user;
    }

    @Autowired
    private UserRepository userRepository;
}
